package com.common.image_loader;

import android.graphics.Bitmap;
import android.os.Handler;

import com.baidu.common.async_http.ThreadPool;
import com.baidu.common.utils.Tools;
import com.common.image_loader.ImageManager.RequestImageListener;

public class ImageIOReadRequest implements Runnable
{
	private String url;
	private Holder holder;
	private Handler hanlder = new Handler();
	
	public static class Holder
	{
		RequestImageListener listener;
		Bitmap bitmap;
		String url;
	}
	
	public ImageIOReadRequest(Holder holder)
	{
		this.url = holder.url;
		this.holder = holder;
	}
	
	@Override
	public void run()
	{
		try
		{
			Bitmap img = LocalImageManager.loadImage(Tools.getMD5(this.url));
			this.holder.bitmap = img;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		if (this.holder != null && this.holder.listener != null)
		{
			this.hanlder.post(new Runnable()
			{
				@Override
				public void run()
				{
					holder.listener.onRecievedImage(holder.bitmap);
				}
			});
		}
	}
	
	public void StartRequest()
	{
		ThreadPool.getInstance().execute(this);
	}
}
